package sedev.shopping.business;

import sedev.shopping.data.SanPhamDAO;
import sedev.shopping.entities.ListSanPham;
import sedev.shopping.entities.SanPham;
import sedev.shopping.util.Paging;
import sedev.shopping.util.SedevUtil;

public class SanPhamService {
	
	public static boolean addData(SanPham data){
		SanPhamDAO dao = new SanPhamDAO();
		return dao.addData(data);
	}
	
	public static boolean updateData(SanPham data){
		SanPhamDAO dao = new SanPhamDAO();
		return dao.updateData(data);
	}
	
	public static boolean removeData(long id){
		SanPhamDAO dao = new SanPhamDAO();
		return dao.removeData(id);
	}
	
	public static SanPham getData(long id){
		SanPhamDAO dao = new SanPhamDAO();
		return dao.getData(id);		
	}
	
	/**
	 * @see For manager
	 */
	public static ListSanPham getListDataByAll(Paging paging){
		StringBuilder field = new StringBuilder();
		field.append(SanPham.Properties.id.name());
		field.append(",").append(SanPham.Properties.user_id.name());
		field.append(",").append(SanPham.Properties.ten_san_pham.name());
		field.append(",").append(SanPham.Properties.hien_thi.name());
		field.append(",").append(SanPham.Properties.ngay_dang.name());
	
		StringBuilder condition = new StringBuilder();
		condition.append("1 AND 1");
		
		StringBuilder order = new StringBuilder();
		order.append(SanPham.Properties.ngay_dang.name()).append(" ").append(SedevUtil.Order.DESC);
		
		SanPhamDAO dao = new SanPhamDAO();
		return dao.getListData(field.toString(), condition.toString(), order.toString(), paging);
	}
	
	/**
	 * @see For manager
	 */
	public static ListSanPham getListDataByUser(long user_id, Paging paging, SedevUtil.ModeDisplay mode_display){
		StringBuilder field = new StringBuilder();
		field.append(SanPham.Properties.id.name());
		field.append(",").append(SanPham.Properties.user_id.name());
		field.append(",").append(SanPham.Properties.ten_san_pham.name());
		field.append(",").append(SanPham.Properties.hien_thi.name());
		field.append(",").append(SanPham.Properties.ngay_dang.name());
	
		StringBuilder condition = new StringBuilder();
		condition.append("1 AND 1");
		condition.append(" AND ").append(SanPham.Properties.user_id.name()).append("=").append(user_id);
		switch (mode_display) {
			case Display:
				condition.append(" AND ").append(SanPham.Properties.hien_thi.name()).append("= 1");
				break;
			case Hide:
				condition.append(" AND ").append(SanPham.Properties.hien_thi.name()).append("= 0");
				break;
			default:
				break;
		}
		StringBuilder order = new StringBuilder();
		order.append(SanPham.Properties.ngay_dang.name()).append(" ").append(SedevUtil.Order.DESC);
		
		SanPhamDAO dao = new SanPhamDAO();
		return dao.getListData(field.toString(), condition.toString(), order.toString(), paging);
	}
	
	/*
	 * Common
	 */
	public static ListSanPham getListDataByGroup(long groupId, Paging paging){
		StringBuilder field = new StringBuilder();
		field.append(SanPham.Properties.id.name());
		field.append(",").append(SanPham.Properties.user_id.name());
		field.append(",").append(SanPham.Properties.ten_san_pham.name());
		field.append(",").append(SanPham.Properties.gia_san_pham.name());
		field.append(",").append(SanPham.Properties.gia_khuyen_mai.name());
		field.append(",").append(SanPham.Properties.don_vi_gia.name());
		field.append(",").append(SanPham.Properties.hinh_anh.name());
		field.append(",").append(SanPham.Properties.vip.name());
		field.append(",").append(SanPham.Properties.danh_gia.name());
		field.append(",").append(SanPham.Properties.luot_xem.name());
		field.append(",").append(SanPham.Properties.ngay_dang.name());
	
		StringBuilder condition = new StringBuilder();
		long timeNow = SedevUtil.getTime();
		condition.append("1 AND 1");
		condition.append(" AND ").append(SanPham.Properties.nhom_san_pham_id.name()).append("=").append(groupId);
		condition.append(" AND ").append(SanPham.Properties.hien_thi.name()).append("= 1");		
		condition.append(" AND ");
		condition.append("(").append(SanPham.Properties.ngay_het_han.name()).append("= 0");
		condition.append(" OR (").append(timeNow).append(">").append(SanPham.Properties.ngay_dang.name());
		condition.append(" AND ").append(timeNow).append("<").append(SanPham.Properties.ngay_het_han.name());
		condition.append("))");
		
		StringBuilder order = new StringBuilder();
		order.append(SanPham.Properties.vip.name()).append(" ").append(SedevUtil.Order.DESC);
		order.append(",").append(SanPham.Properties.ngay_dang.name()).append(" ").append(SedevUtil.Order.DESC);
		
		SanPhamDAO dao = new SanPhamDAO();
		return dao.getListData(field.toString(), condition.toString(), order.toString(), paging);
	}
	
	public static ListSanPham getListDataByType(long typeId, Paging paging){
		StringBuilder field = new StringBuilder();
		field.append(SanPham.Properties.id.name());
		field.append(",").append(SanPham.Properties.user_id.name());
		field.append(",").append(SanPham.Properties.ten_san_pham.name());
		field.append(",").append(SanPham.Properties.gia_san_pham.name());
		field.append(",").append(SanPham.Properties.gia_khuyen_mai.name());
		field.append(",").append(SanPham.Properties.don_vi_gia.name());
		field.append(",").append(SanPham.Properties.hinh_anh.name());
		field.append(",").append(SanPham.Properties.vip.name());
		field.append(",").append(SanPham.Properties.danh_gia.name());
		field.append(",").append(SanPham.Properties.luot_xem.name());
		field.append(",").append(SanPham.Properties.ngay_dang.name());
	
		StringBuilder condition = new StringBuilder();
		long timeNow = SedevUtil.getTime();
		condition.append("1 AND 1");
		condition.append(" AND ").append(SanPham.Properties.loai_san_pham_id.name()).append("=").append(typeId);
		condition.append(" AND ").append(SanPham.Properties.hien_thi.name()).append("= 1");		
		condition.append(" AND ");
		condition.append("(").append(SanPham.Properties.ngay_het_han.name()).append("= 0");
		condition.append(" OR (").append(timeNow).append(">").append(SanPham.Properties.ngay_dang.name());
		condition.append(" AND ").append(timeNow).append("<").append(SanPham.Properties.ngay_het_han.name());
		condition.append("))");
		
		StringBuilder order = new StringBuilder();
		order.append(SanPham.Properties.vip.name()).append(" ").append(SedevUtil.Order.DESC);
		order.append(",").append(SanPham.Properties.ngay_dang.name()).append(" ").append(SedevUtil.Order.DESC);
		
		SanPhamDAO dao = new SanPhamDAO();
		return dao.getListData(field.toString(), condition.toString(), order.toString(), paging);
	}
	
	public static ListSanPham getListDataVip(Paging paging){
		StringBuilder field = new StringBuilder();
		field.append(SanPham.Properties.id.name());		
		field.append(",").append(SanPham.Properties.ten_san_pham.name());
		field.append(",").append(SanPham.Properties.gia_san_pham.name());
		field.append(",").append(SanPham.Properties.gia_khuyen_mai.name());
		field.append(",").append(SanPham.Properties.don_vi_gia.name());
		field.append(",").append(SanPham.Properties.hinh_anh.name());
		field.append(",").append(SanPham.Properties.vip.name());
		field.append(",").append(SanPham.Properties.danh_gia.name());
		field.append(",").append(SanPham.Properties.luot_xem.name());		
		field.append(",").append(SanPham.Properties.ngay_dang.name());
	
		StringBuilder condition = new StringBuilder();
		long timeNow = SedevUtil.getTime();
		condition.append("1 AND 1");
		condition.append(" AND ").append(SanPham.Properties.hien_thi.name()).append("= 1");
		condition.append(" AND ").append(SanPham.Properties.vip.name()).append("> 0");
		condition.append(" AND ");
		condition.append("(").append(SanPham.Properties.ngay_het_han.name()).append("= 0");
		condition.append(" OR (").append(timeNow).append(">").append(SanPham.Properties.ngay_dang.name());
		condition.append(" AND ").append(timeNow).append("<").append(SanPham.Properties.ngay_het_han.name());
		condition.append("))");
		
		StringBuilder order = new StringBuilder();
		order.append(SanPham.Properties.vip.name()).append(" ").append(SedevUtil.Order.DESC);
		order.append(",").append(SanPham.Properties.ngay_dang.name()).append(" ").append(SedevUtil.Order.DESC);
		
		SanPhamDAO dao = new SanPhamDAO();
		return dao.getListData(field.toString(), condition.toString(), order.toString(), paging);
	}
	
	public static ListSanPham getListDataNewest(Paging paging){
		StringBuilder field = new StringBuilder();
		field.append(SanPham.Properties.id.name());		
		field.append(",").append(SanPham.Properties.ten_san_pham.name());
		field.append(",").append(SanPham.Properties.gia_san_pham.name());
		field.append(",").append(SanPham.Properties.gia_khuyen_mai.name());
		field.append(",").append(SanPham.Properties.don_vi_gia.name());
		field.append(",").append(SanPham.Properties.hinh_anh.name());
		field.append(",").append(SanPham.Properties.vip.name());
		field.append(",").append(SanPham.Properties.danh_gia.name());
		field.append(",").append(SanPham.Properties.luot_xem.name());		
		field.append(",").append(SanPham.Properties.ngay_dang.name());
	
		StringBuilder condition = new StringBuilder();
		long timeNow = SedevUtil.getTime();
		condition.append("1 AND 1");
		condition.append(" AND ").append(SanPham.Properties.hien_thi.name()).append("= 1");
		condition.append(" AND ");
		condition.append("(").append(SanPham.Properties.ngay_het_han.name()).append("= 0");
		condition.append(" OR (").append(timeNow).append(">").append(SanPham.Properties.ngay_dang.name());
		condition.append(" AND ").append(timeNow).append("<").append(SanPham.Properties.ngay_het_han.name());
		condition.append("))");
		
		StringBuilder order = new StringBuilder();
		order.append(SanPham.Properties.ngay_dang.name()).append(" ").append(SedevUtil.Order.DESC);
		
		SanPhamDAO dao = new SanPhamDAO();
		return dao.getListData(field.toString(), condition.toString(), order.toString(), paging);
	}
	
	public static ListSanPham getListDataSameGroup(long id, long groupId, Paging paging){
		StringBuilder field = new StringBuilder();
		field.append(SanPham.Properties.id.name());		
		field.append(",").append(SanPham.Properties.ten_san_pham.name());
		field.append(",").append(SanPham.Properties.gia_san_pham.name());
		field.append(",").append(SanPham.Properties.gia_khuyen_mai.name());
		field.append(",").append(SanPham.Properties.don_vi_gia.name());
		field.append(",").append(SanPham.Properties.hinh_anh.name());
		field.append(",").append(SanPham.Properties.vip.name());
		field.append(",").append(SanPham.Properties.danh_gia.name());
		field.append(",").append(SanPham.Properties.luot_xem.name());		
		field.append(",").append(SanPham.Properties.ngay_dang.name());
	
		StringBuilder condition = new StringBuilder();
		long timeNow = SedevUtil.getTime();
		condition.append("1 AND 1");
		condition.append(" AND ").append(SanPham.Properties.hien_thi.name()).append("= 1");
		condition.append(" AND ").append(SanPham.Properties.id.name()).append("!=").append(id);
		condition.append(" AND ").append(SanPham.Properties.nhom_san_pham_id.name()).append("=").append(groupId);
		condition.append(" AND ");
		condition.append("(").append(SanPham.Properties.ngay_het_han.name()).append("= 0");
		condition.append(" OR (").append(timeNow).append(">").append(SanPham.Properties.ngay_dang.name());
		condition.append(" AND ").append(timeNow).append("<").append(SanPham.Properties.ngay_het_han.name());
		condition.append("))");
		
		StringBuilder order = new StringBuilder();
		order.append(SanPham.Properties.ngay_dang.name()).append(" ").append(SedevUtil.Order.DESC);
		
		SanPhamDAO dao = new SanPhamDAO();
		return dao.getListData(field.toString(), condition.toString(), order.toString(), paging);
	}
	
	public static ListSanPham getListDataByUserIdAndOtherProductId(long productId, long userId, Paging paging){
		StringBuilder field = new StringBuilder();
		field.append(SanPham.Properties.id.name());		
		field.append(",").append(SanPham.Properties.ten_san_pham.name());
		field.append(",").append(SanPham.Properties.gia_san_pham.name());
		field.append(",").append(SanPham.Properties.gia_khuyen_mai.name());
		field.append(",").append(SanPham.Properties.don_vi_gia.name());
		field.append(",").append(SanPham.Properties.hinh_anh.name());
		field.append(",").append(SanPham.Properties.vip.name());
		field.append(",").append(SanPham.Properties.danh_gia.name());
		field.append(",").append(SanPham.Properties.luot_xem.name());		
		field.append(",").append(SanPham.Properties.ngay_dang.name());
	
		StringBuilder condition = new StringBuilder();
		long timeNow = SedevUtil.getTime();
		condition.append("1 AND 1");
		condition.append(" AND ").append(SanPham.Properties.hien_thi.name()).append("= 1");
		condition.append(" AND ").append(SanPham.Properties.id.name()).append("!=").append(productId);
		condition.append(" AND ").append(SanPham.Properties.user_id.name()).append("=").append(userId);
		condition.append(" AND ");
		condition.append("(").append(SanPham.Properties.ngay_het_han.name()).append("= 0");
		condition.append(" OR (").append(timeNow).append(">").append(SanPham.Properties.ngay_dang.name());
		condition.append(" AND ").append(timeNow).append("<").append(SanPham.Properties.ngay_het_han.name());
		condition.append("))");
		
		StringBuilder order = new StringBuilder();
		order.append(SanPham.Properties.ngay_dang.name()).append(" ").append(SedevUtil.Order.DESC);
		
		SanPhamDAO dao = new SanPhamDAO();
		return dao.getListData(field.toString(), condition.toString(), order.toString(), paging);
	}
	
	public static ListSanPham search(long userId, String keyword, Paging paging){
		StringBuilder field = new StringBuilder();
		field.append(SanPham.Properties.id.name());
		field.append(",").append(SanPham.Properties.user_id.name());
		field.append(",").append(SanPham.Properties.ten_san_pham.name());
		field.append(",").append(SanPham.Properties.gia_san_pham.name());
		field.append(",").append(SanPham.Properties.gia_khuyen_mai.name());
		field.append(",").append(SanPham.Properties.don_vi_gia.name());
		field.append(",").append(SanPham.Properties.hinh_anh.name());
		field.append(",").append(SanPham.Properties.vip.name());
		field.append(",").append(SanPham.Properties.danh_gia.name());
		field.append(",").append(SanPham.Properties.luot_xem.name());
		field.append(",").append(SanPham.Properties.ngay_dang.name());
	
		StringBuilder condition = new StringBuilder();
		long timeNow = SedevUtil.getTime();
		condition.append("1 AND 1");
		if(userId > 0){
			condition.append(" AND ").append(SanPham.Properties.user_id.name()).append("=").append(userId);
		}		
		condition.append(" AND ").append(SanPham.Properties.hien_thi.name()).append("= 1");		
		condition.append(" AND ");
		condition.append("(").append(SanPham.Properties.ngay_het_han.name()).append("= 0");
		condition.append(" OR (").append(timeNow).append(">").append(SanPham.Properties.ngay_dang.name());
		condition.append(" AND ").append(timeNow).append("<").append(SanPham.Properties.ngay_het_han.name());
		condition.append("))");
		condition.append(" AND ");
		condition.append("(").append(SanPham.Properties.ma_san_pham.name()).append(" like ").append("'%").append(keyword).append("%'");
		condition.append(" OR ").append(SanPham.Properties.ten_san_pham.name()).append(" like ").append("'%").append(keyword).append("%'");
		condition.append(")");
		
		StringBuilder order = new StringBuilder();		
		order.append(SanPham.Properties.ngay_dang.name()).append(" ").append(SedevUtil.Order.DESC);
		
		SanPhamDAO dao = new SanPhamDAO();
		return dao.getListData(field.toString(), condition.toString(), order.toString(), paging);
	}
	
	/*
	 * Of User
	 */
	/**
	 * @see For frontend
	 */
	public static ListSanPham getListDataVipByUser(long userId, Paging paging){
		StringBuilder field = new StringBuilder();
		field.append(SanPham.Properties.id.name());		
		field.append(",").append(SanPham.Properties.ten_san_pham.name());
		field.append(",").append(SanPham.Properties.gia_san_pham.name());
		field.append(",").append(SanPham.Properties.gia_khuyen_mai.name());
		field.append(",").append(SanPham.Properties.don_vi_gia.name());
		field.append(",").append(SanPham.Properties.hinh_anh.name());
		field.append(",").append(SanPham.Properties.vip.name());
		field.append(",").append(SanPham.Properties.danh_gia.name());
		field.append(",").append(SanPham.Properties.luot_xem.name());		
		field.append(",").append(SanPham.Properties.ngay_dang.name());
	
		StringBuilder condition = new StringBuilder();
		long timeNow = SedevUtil.getTime();
		condition.append("1 AND 1");
		condition.append(" AND ").append(SanPham.Properties.user_id.name()).append("=").append(userId);
		condition.append(" AND ").append(SanPham.Properties.hien_thi.name()).append("= 1");
		condition.append(" AND ").append(SanPham.Properties.vip.name()).append("> 0");
		condition.append(" AND ");
		condition.append("(").append(SanPham.Properties.ngay_het_han.name()).append("= 0");
		condition.append(" OR (").append(timeNow).append(">").append(SanPham.Properties.ngay_dang.name());
		condition.append(" AND ").append(timeNow).append("<").append(SanPham.Properties.ngay_het_han.name());
		condition.append("))");
		
		StringBuilder order = new StringBuilder();
		order.append(SanPham.Properties.vip.name()).append(" ").append(SedevUtil.Order.DESC);
		order.append(",").append(SanPham.Properties.ngay_dang.name()).append(" ").append(SedevUtil.Order.DESC);
		
		SanPhamDAO dao = new SanPhamDAO();
		return dao.getListData(field.toString(), condition.toString(), order.toString(), paging);
	}
	
	public static ListSanPham getListDataNewestByUser(long userId, Paging paging){
		StringBuilder field = new StringBuilder();
		field.append(SanPham.Properties.id.name());		
		field.append(",").append(SanPham.Properties.ten_san_pham.name());
		field.append(",").append(SanPham.Properties.gia_san_pham.name());
		field.append(",").append(SanPham.Properties.gia_khuyen_mai.name());
		field.append(",").append(SanPham.Properties.don_vi_gia.name());
		field.append(",").append(SanPham.Properties.hinh_anh.name());
		field.append(",").append(SanPham.Properties.vip.name());
		field.append(",").append(SanPham.Properties.danh_gia.name());
		field.append(",").append(SanPham.Properties.luot_xem.name());		
		field.append(",").append(SanPham.Properties.ngay_dang.name());
	
		StringBuilder condition = new StringBuilder();
		long timeNow = SedevUtil.getTime();
		condition.append("1 AND 1");
		condition.append(" AND ").append(SanPham.Properties.user_id.name()).append("=").append(userId);
		condition.append(" AND ").append(SanPham.Properties.hien_thi.name()).append("= 1");
		condition.append(" AND ");
		condition.append("(").append(SanPham.Properties.ngay_het_han.name()).append("= 0");
		condition.append(" OR (").append(timeNow).append(">").append(SanPham.Properties.ngay_dang.name());
		condition.append(" AND ").append(timeNow).append("<").append(SanPham.Properties.ngay_het_han.name());
		condition.append("))");
		
		StringBuilder order = new StringBuilder();
		order.append(SanPham.Properties.ngay_dang.name()).append(" ").append(SedevUtil.Order.DESC);
		
		SanPhamDAO dao = new SanPhamDAO();
		return dao.getListData(field.toString(), condition.toString(), order.toString(), paging);
	}
	
	public static ListSanPham getListDataNewestByProductType(long loai_san_pham_id, Paging paging){
		StringBuilder field = new StringBuilder();
		field.append(SanPham.Properties.id.name());		
		field.append(",").append(SanPham.Properties.ten_san_pham.name());
		field.append(",").append(SanPham.Properties.gia_san_pham.name());
		field.append(",").append(SanPham.Properties.gia_khuyen_mai.name());
		field.append(",").append(SanPham.Properties.hinh_anh.name());
		field.append(",").append(SanPham.Properties.vip.name());
		field.append(",").append(SanPham.Properties.danh_gia.name());
		field.append(",").append(SanPham.Properties.luot_xem.name());		
		field.append(",").append(SanPham.Properties.ngay_dang.name());
	
		StringBuilder condition = new StringBuilder();
		condition.append("1 AND 1");
		condition.append(" AND ").append(SanPham.Properties.loai_san_pham_id.name()).append("=").append(loai_san_pham_id);
		condition.append(" AND ").append(SanPham.Properties.hien_thi.name()).append("= 1");
		condition.append(" AND ");
		condition.append("(").append(SanPham.Properties.ngay_het_han.name()).append("= 0");
		condition.append(" OR ").append(SedevUtil.getTime()).append("<").append(SanPham.Properties.ngay_het_han.name());
		condition.append(")");
			
		StringBuilder order = new StringBuilder();
		order.append(SanPham.Properties.ngay_dang.name()).append(" ").append(SedevUtil.Order.DESC);
		
		SanPhamDAO dao = new SanPhamDAO();
		return dao.getListData(field.toString(), condition.toString(), order.toString(), paging);
	}
	
	public static ListSanPham getListDataVipByProductType(long loai_san_pham_id, Paging paging){
		StringBuilder field = new StringBuilder();
		field.append(SanPham.Properties.id.name());		
		field.append(",").append(SanPham.Properties.ten_san_pham.name());
		field.append(",").append(SanPham.Properties.gia_san_pham.name());
		field.append(",").append(SanPham.Properties.gia_khuyen_mai.name());
		field.append(",").append(SanPham.Properties.hinh_anh.name());
		field.append(",").append(SanPham.Properties.vip.name());
		field.append(",").append(SanPham.Properties.danh_gia.name());
		field.append(",").append(SanPham.Properties.luot_xem.name());		
		field.append(",").append(SanPham.Properties.ngay_dang.name());
	
		StringBuilder condition = new StringBuilder();
		condition.append("1 AND 1");
		condition.append(" AND ").append(SanPham.Properties.loai_san_pham_id.name()).append("=").append(loai_san_pham_id);
		condition.append(" AND ").append(SanPham.Properties.hien_thi.name()).append("= 1");
		condition.append(" AND ").append(SanPham.Properties.vip.name()).append(">").append(0);		
		condition.append(" AND ");
		condition.append("(").append(SanPham.Properties.ngay_het_han.name()).append("= 0");
		condition.append(" OR ").append(SedevUtil.getTime()).append("<").append(SanPham.Properties.ngay_het_han.name());
		condition.append(")");
			
		StringBuilder order = new StringBuilder();
		order.append(SanPham.Properties.vip.name()).append(" ").append(SedevUtil.Order.DESC);
		order.append(",").append(SanPham.Properties.ngay_dang.name()).append(" ").append(SedevUtil.Order.DESC);
		
		SanPhamDAO dao = new SanPhamDAO();
		return dao.getListData(field.toString(), condition.toString(), order.toString(), paging);
	}
	
	public static ListSanPham getListDataPopularByUser(long user_id, Paging paging){
		StringBuilder field = new StringBuilder();
		field.append(SanPham.Properties.id.name());		
		field.append(",").append(SanPham.Properties.ten_san_pham.name());
		field.append(",").append(SanPham.Properties.gia_san_pham.name());
		field.append(",").append(SanPham.Properties.gia_khuyen_mai.name());
		field.append(",").append(SanPham.Properties.hinh_anh.name());
		field.append(",").append(SanPham.Properties.vip.name());
		field.append(",").append(SanPham.Properties.danh_gia.name());
		field.append(",").append(SanPham.Properties.luot_xem.name());		
		field.append(",").append(SanPham.Properties.ngay_dang.name());
	
		StringBuilder condition = new StringBuilder();
		condition.append("1 AND 1");
		condition.append(" AND ").append(SanPham.Properties.user_id.name()).append("=").append(user_id);
		condition.append(" AND ").append(SanPham.Properties.hien_thi.name()).append("= 1");
		condition.append(" AND ");
		condition.append("(").append(SanPham.Properties.ngay_het_han.name()).append("= 0");
		condition.append(" OR ").append(SedevUtil.getTime()).append("<").append(SanPham.Properties.ngay_het_han.name());
		condition.append(")");
			
		StringBuilder order = new StringBuilder();
		order.append(SanPham.Properties.luot_xem.name()).append(" ").append(SedevUtil.Order.DESC);
		order.append(",").append(SanPham.Properties.ngay_dang.name()).append(" ").append(SedevUtil.Order.DESC);
		
		SanPhamDAO dao = new SanPhamDAO();
		return dao.getListData(field.toString(), condition.toString(), order.toString(), paging);
	}
	
	public static ListSanPham getListDataPopularByProductType(long loai_san_pham_id, Paging paging){
		StringBuilder field = new StringBuilder();
		field.append(SanPham.Properties.id.name());		
		field.append(",").append(SanPham.Properties.ten_san_pham.name());
		field.append(",").append(SanPham.Properties.gia_san_pham.name());
		field.append(",").append(SanPham.Properties.gia_khuyen_mai.name());
		field.append(",").append(SanPham.Properties.hinh_anh.name());
		field.append(",").append(SanPham.Properties.vip.name());
		field.append(",").append(SanPham.Properties.danh_gia.name());
		field.append(",").append(SanPham.Properties.luot_xem.name());		
		field.append(",").append(SanPham.Properties.ngay_dang.name());
	
		StringBuilder condition = new StringBuilder();
		condition.append("1 AND 1");
		condition.append(" AND ").append(SanPham.Properties.loai_san_pham_id.name()).append("=").append(loai_san_pham_id);
		condition.append(" AND ").append(SanPham.Properties.hien_thi.name()).append("= 1");
		condition.append(" AND ");
		condition.append("(").append(SanPham.Properties.ngay_het_han.name()).append("= 0");
		condition.append(" OR ").append(SedevUtil.getTime()).append("<").append(SanPham.Properties.ngay_het_han.name());
		condition.append(")");
		
		StringBuilder order = new StringBuilder();
		order.append(SanPham.Properties.luot_xem.name()).append(" ").append(SedevUtil.Order.DESC);
		order.append(",").append(SanPham.Properties.ngay_dang.name()).append(" ").append(SedevUtil.Order.DESC);
		
		SanPhamDAO dao = new SanPhamDAO();
		return dao.getListData(field.toString(), condition.toString(), order.toString(), paging);
	}	
}
